import asyncio
import websockets
import json
async def connect():
    uri = "wss://www.ps8989.com/sports-websocket/ws?token=AAAAAARs7BMAAAGVs58YqJlxjIZTs_QrsiUg_i278AokxTQIddkwGjdwxvrvIidP&ulp=dVNQSUk3UTlEdGF2MVVJbTNUclptL2lGK3NqSzE5eVJkSEZ0c0NEb1VVZzJDU1NNV2tST0xZdzFWOEtFZ21mQXwwNDBhNTkzY2Q3YzE4MDdlYmM2MWFiNGU1YThiZTY5OA%3D%3D"
    body1 ={"type":"SUBSCRIBE","destination":"LIVE_SCORE","body":{"locale":"zh_CN"}}
    body2 ={"type":"PONG","destination":"ALL"}
    first = True
    while True:
        try:
            async with websockets.connect(uri) as websocket:
                while True:
                    # 发送消息
                    body = ''
                    if first:
                        body = body1
                        first = False
                    else:
                        body = body2
                    print(json.dumps(body))
                    await websocket.send( json.dumps(body))
                    # 接收消息
                    response = await websocket.recv()
                    print(response)
                    # 等待一段时间
                    await asyncio.sleep(30)
        except websockets.ConnectionClosed as e:
            print("Connection closed, retrying in 5 seconds...", e)
            await asyncio.sleep(5)

if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(connect())